package com.example.demo_server;



import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;



public class StudentService extends Service {
    private List<Student> mStudentList;
    private RemoteCallbackList<IStudentChangeListener> mListeners;
    private DBHelper mDBHelper;

    @Override
    public void onCreate() {
        super.onCreate();
        mStudentList = new ArrayList<>();
        mListeners = new RemoteCallbackList<>();
        mDBHelper = DBHelper.getInstance(this);
        Log.d("asdf", "server onCreate");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return stub;
    }

    IStudentService.Stub stub = new IStudentService.Stub() {
        @Override
        public void addListener(IStudentChangeListener listener) throws RemoteException {
            if (mListeners != null && listener != null) {
                mListeners.register(listener);
            }
        }

        @Override
        public void addStudent(Student student) throws RemoteException {
            Log.d("asdf", "server addStudent");
            if (mDBHelper != null) {
                Log.d("asdf", "server addStudent " + student.getNumber());
                mDBHelper.insertStudent(student);
                notifyCountChanged(getStudentCount());
            }
        }

        @Override
        public void deleteStudent(int id) throws RemoteException {
            Log.d("asdf", "server deleteStudent");
            if (mDBHelper == null) {
                notifyNotFound();
                return;
            }
            mDBHelper.deleteStudentById(id);
            notifyCountChanged(getStudentCount());
        }

        @Override
        public void deleteStudentByNumber(int number) throws RemoteException {
            Log.d("asdf", "server deleteStudentByNumber");
            if (mDBHelper == null) {
                notifyNotFound();
                return;
            }
            mDBHelper.deleteStudentByNumber(number);
            notifyCountChanged(getStudentCount());
        }

        @Override
        public List<Student> getStudentList() throws RemoteException {
            Log.d("asdf", "server getStudentList");
            if (mDBHelper == null) {
                notifyNotFound();
                return null;
            }
            mStudentList = mDBHelper.getStudentList();
            return mStudentList;
        }

        @Override
        public int getStudentCount() throws RemoteException {
            Log.d("asdf", "server getStudentCount");
            if (mDBHelper != null) {
                mStudentList = mDBHelper.getStudentList();
                if (mStudentList != null) {
                    return mStudentList.size();
                }
            }
            return 0;
        }

        @Override
        public Student getStudentByNumber(int number) throws RemoteException {
            Log.d("asdf", "server getStudentByNumber");
            if (mDBHelper == null) {
                notifyNotFound();
                return null;
            }
            Student student = mDBHelper.getStudentByNumber(number);
            return student;
        }
    };

    public void notifyCountChanged(int count) {
        if (mListeners == null) {
            return;
        }
        final int N = mListeners.beginBroadcast();
        for (int i = 0; i < N; i++) {
            try {
                Log.d("asdf", "notifyCountChanged = " + count);
                mListeners.getBroadcastItem(i).onStudentCountChanged(count);
            } catch (RemoteException e) {
                Log.d("asdf", "notifyCountChanged exception!");
            }
        }
        mListeners.finishBroadcast();
    }

    public void notifyNotFound() {
        if (mListeners == null) {
            return;
        }
        final int N = mListeners.beginBroadcast();
        for (int i = 0; i < N; i++) {
            try {
                mListeners.getBroadcastItem(i).onStudentNotFound();
            } catch (RemoteException e) {
                Log.d("asdf", "notifyNotFound exception!");
            }
        }
        mListeners.finishBroadcast();
    }
}